package com.syh.jn.scm.service.impl.driver.logistics;

import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.downstream.entity.logistics.DownLogisticsBill;
import com.syh.jn.scm.domain.downstream.event.DownLogisticsBillEvent;
import com.syh.jn.scm.domain.downstream.repository.logistics.DownLogisticsBillRepository;
import com.syh.jn.scm.domain.driver.entity.logistics.DriverLogisticsBill;
import com.syh.jn.scm.domain.driver.event.DriverLogisticsBillEvent;
import com.syh.jn.scm.domain.driver.repository.logistics.DriverLogisticsBillRepository;
import com.syh.jn.scm.domain.supplier.entity.purchase.PurchaseOrder;
import com.syh.jn.scm.domain.supplier.repository.purchase.PurchaseOrderReadRepository;
import com.syh.jn.scm.dto.driver.logistics.DriverLogisticsBillItemVo;
import com.syh.jn.scm.dto.driver.logistics.DriverLogisticsBillMto;
import com.syh.jn.scm.dto.driver.logistics.DriverLogisticsBillSto;
import com.syh.jn.scm.dto.driver.logistics.DriverLogisticsBillVo;
import com.syh.jn.scm.service.api.driver.logistics.DriverLogisticsBillService;
import com.syh.jn.scm.service.api.driver.logistics.enums.DriverLogisticsBillEnums;
import com.syh.jn.scm.service.api.supplier.purchase.enums.PurchaseOrderEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by soci.song on 2018/11/8.
 */
@Service
@Transactional
public class DriverLogisticsBillServiceImpl extends AbstractServiceImpl<DriverLogisticsBillSto,DriverLogisticsBillMto,DriverLogisticsBillVo,DriverLogisticsBill,Long> implements DriverLogisticsBillService {

    @Autowired
    private DriverLogisticsBillRepository driverLogisticsBillRepository;

    @Override
    protected AggregateRepository getAggregateRepository() {
        return this.driverLogisticsBillRepository;
    }


    @Override
    public void submitDelivery(DriverLogisticsBillSto sto) {
        DriverLogisticsBill logisticsBill = new DriverLogisticsBill(sto);
        logisticsBill.delivery();
        driverLogisticsBillRepository.updateStatus(logisticsBill);
        this.publicEvent(DriverLogisticsBillEvent.bulidDriverLogisticsBillDelivery(new Object[]{logisticsBill}));
        //如果是直采直销 抛个直采直销事件
        //TODO 直采直销发货不同步
//        if (logisticsBill.getOrderType().equals(DriverLogisticsBillEnums.OrderType.ORDER_TYPE_PURCHASE.getStatus())) {
//            PurchaseOrderReadRepository purchaseOrderReadRepository = (PurchaseOrderReadRepository) SpringContextUtils.getBean(PurchaseOrderReadRepository.class);
//            PurchaseOrder purchaseOrder = purchaseOrderReadRepository.getById(logisticsBill.getOrderId());
//            if (purchaseOrder.getOrderType().equals(PurchaseOrderEnums.OrderType.DIRECT_PURCHASE_AND_SALE.getStatus())) {
//                UpLogisticsBillRepository upLogisticsBillRepository = (UpLogisticsBillRepository) SpringContextUtils.getBean(UpLogisticsBillRepository.class);
//                UpLogisticsBill upLogisticsBill = upLogisticsBillRepository.getById(logisticsBill.getId());
//                this.publicEvent(UpLogisticsBillEvent.bulidPurchaseSaleLogisticsBillDelivery(new Object[]{upLogisticsBill}));
//            }
//        }
    }

    @Override
    public void submitCollect(DriverLogisticsBillSto sto) {
        DriverLogisticsBill logisticsBill = new DriverLogisticsBill(sto);
        //如果是直采直销 抛个直采直销事件
        if (logisticsBill.getOrderType().equals(DriverLogisticsBillEnums.OrderType.ORDER_TYPE_PURCHASE.getStatus())) {
            PurchaseOrderReadRepository purchaseOrderReadRepository = (PurchaseOrderReadRepository) SpringContextUtils.getBean("purchaseOrderRepositoryImpl");
            PurchaseOrder purchaseOrder = purchaseOrderReadRepository.getById(logisticsBill.getOrderId());
            if (purchaseOrder.getOrderType().equals(PurchaseOrderEnums.OrderType.DIRECT_PURCHASE_AND_SALE.getStatus())) {
                DownLogisticsBillRepository downLogisticsBillRepository = (DownLogisticsBillRepository) SpringContextUtils.getBean(DownLogisticsBillRepository.class);
                Wrapper we = Wrapper.build();
                we.append(Restrictions.eq("subjectBillId", logisticsBill.getId()));
                DownLogisticsBill downLogisticsBill = downLogisticsBillRepository.getOne(we);
                sto.setId(downLogisticsBill.getId());
                //直采直销改为先对下游采购物流单收货，再抛出直采直销收货事件
                logisticsBill = new DriverLogisticsBill(sto);
                this.publicEvent(DownLogisticsBillEvent.bulidPurchaseSaleLogisticsBillCollected(new Object[]{downLogisticsBill}));
            }
        }
        logisticsBill.collect();
        driverLogisticsBillRepository.updateStatus(logisticsBill);
        this.publicEvent(DriverLogisticsBillEvent.bulidDriverLogisticsBillCollected(new Object[]{logisticsBill}));
    }

    @Override
    public List<DriverLogisticsBillItemVo> getItemsByBillId(Long id) {
        return driverLogisticsBillRepository.getItemsByBillId(id);
    }
}
